/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.importpremium.service.impl;

import com.kmcsoft.emaster.entity.Employee;
import com.kmcsoft.goldshop.importpremium.service.ImportPremiumService;
import com.kmcsoft.goldshop.model.master.PremiumJewelryType;
import com.kmcsoft.goldshop.model.master.ProductPremiumJewelry;
import com.kmcsoft.master.defaultservice.DefaultGenericService;
import com.kmcsoft.utils.DateUtil;
import java.util.Collection;
import java.util.Date;
import javax.jdo.Query;

/**
 *
 * @author kmcsoft02
 */
public class JdoImportPremiumService extends DefaultGenericService implements ImportPremiumService{

    //<editor-fold defaultstate="collapsed" desc="getProductPremiumJewelryById">
    public ProductPremiumJewelry getProductPremiumJewelryById(long id) {
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductPremiumJewelry_All");
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductStatusHistory_CreatedBy");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(ProductPremiumJewelry.class, id));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getRecentProduct">
    public ProductPremiumJewelry getRecentProduct(Date date, long empId) {
        Query query = getPersistenceManager().newQuery(ProductPremiumJewelry.class);
        query.setFilter("createdDate>=pDate && createdBy.id==pId && productType == pType");
        query.declareImports("import java.util.Date");
        query.declareParameters("Date pDate, Long pId, Integer pType");
        query.setOrdering("createdDate descending");
        Collection<ProductPremiumJewelry> objects = (Collection<ProductPremiumJewelry>) query.execute(DateUtil.beginOfDate(date), (Long) empId,(Integer) ProductPremiumJewelry.TYPE_PREMIUMJEWELRY);
        if (objects.isEmpty()) {
            return null;
        }
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductPremiumJewelry_All");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopy(objects.iterator().next());
    }
    //</editor-fold>

    public Collection<Employee> getlabeledEmployees() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(Employee.class));
    }

    public Collection<PremiumJewelryType> getJewelryTypes() {
       getPersistenceManager().getFetchPlan().addGroup("detach_PremiumJewelryType_All");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(PremiumJewelryType.class));
    }
    
}
